<?php
namespace App\Http\Controllers\Login;
use App\Http\Controllers\ApiBaseController;
use Illuminate\Http\Request;
use App\Models\AdminModels\Industry;
class IndustryAjaxController extends ApiBaseController {
  public function __construct(Request $request) {
    parent::__construct($request);
  }
  
  /**
   * 行业列表
   */
  public function dataList(){
    $oIndustry = new Industry();
    $result = $oIndustry->getPageData($this->params);
    echo json_encode($result);
  }
  
  /**
   * 删除项标签
   */
  public function deleteData(){
    if(isset($this->params['id']) && intval($this->params['id']) > 0 && isset($this->params['status'])){
      $oIndustry = new Industry();
      if($oIndustry->db_update('',$this->params['id'],['status'=>$this->params['status']])){
        $this->data = true;
      }else{
        $this->code = 1;
        $this->msg = '操作失败';
      }
    }else{
      $this->code = 1;
      $this->msg = '缺少数据';
    }
    $this->boot();
  }

  public function getData(){
    if(isset($this->params['id']) && intval($this->params['id']) > 0){
      $oIndustry = new Industry();
      $iData = $oIndustry->db_get(['id'=>['=',intval($this->params['id'])],'status'=>['>=',0]]);
      if(!empty($iData)){
        $this->data = $iData;
      }else{
        $this->code = 1;
        $this->msg = '数据不存在或已删除';
      }
    }else{
      $this->code = 1;
      $this->msg = '缺少数据';
    }
    $this->boot();
  }
  
  /**
   * 新增标签
   */
  public function dataSave(Request $request){
    if(isset($this->params['name']) && !empty($this->params['name']) && isset($this->params['remark']) && isset($this->params['sort'])){
      $oIndustry = new Industry();
      $iData = $oIndustry->db_get(['name'=>['=',$this->params['name']],'status'=>['>=',0]]);
      $id = (isset($this->params['id']) && intval($this->params['id']) > 0) ? intval($this->params['id']) : 0;
      if(!empty($iData)){
        if($id > 0 && $iData['id'] == $id){
          $data = [];
          $data['name'] = trim($this->params['name']);
          $data['remark'] = trim($this->params['remark']);
          $data['sort'] = intval($this->params['sort']);
          $data['update_time'] = date('Y-m-d H:i:s');
          $oIndustry->db_update('',$id,$data);
        }else{
          $this->code = 1;
          $this->msg = '行业名称已存在';
        }
      }else{
        if($id > 0){
          $data = [];
          $data['name'] = trim($this->params['name']);
          $data['remark'] = trim($this->params['remark']);
          $data['sort'] = intval($this->params['sort']);
          $data['update_time'] = date('Y-m-d H:i:s');
          $oIndustry->db_update('',$id,$data);
        }else{
          $data = [];
          $data['name'] = trim($this->params['name']);
          $data['remark'] = trim($this->params['remark']);
          $data['sort'] = intval($this->params['sort']);
          $data['admin_id'] = $request->session()->get('user')[0]['admin_id'];
          $data['update_time'] = $data['create_time'] = date('Y-m-d H:i:s');
          $oIndustry->db_insert($data);
        }
      }
    }else{
      $this->code = 1;
      $this->msg = '缺少数据';
    }
    $this->boot();
  }
}